package com.dst.controller;

import com.dst.eneity.Brand;
import com.dst.eneity.Phone;
import com.dst.service.BrandService;
import com.dst.service.PhoneService;
import com.dst.service.impl.BrandServiceImpl;
import com.dst.service.impl.PhoneServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet(urlPatterns = "/phone/selectAllController")
public class selectByLimitController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String lpage = req.getParameter("localpage");
        int localpage=1;
        if(lpage!=null){
                localpage=Integer.parseInt(lpage);
        }
        int pageSize=3;
        int start=(localpage-1)*pageSize;
        PhoneService phoneService = new PhoneServiceImpl();
        List<Phone> phone = phoneService.selectLimitPhoneService(start, pageSize);
        BrandService brandService = new BrandServiceImpl();
        List<Brand> brands = brandService.selectAllBrandService();
        req.setAttribute("phone",phone);
        int i = phoneService.selectCount();
        int totalPage=(i%pageSize)==0 ? i/pageSize : (i/pageSize)+1;
        req.setAttribute("totalpage",totalPage);
        req.setAttribute("localpage",localpage);
        HttpSession session = req.getSession();
        session.setAttribute("brands",brands);
        req.getRequestDispatcher("/phone-list.jsp").forward(req,resp);
    }
}
